<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\admin\model\Community as CommunityModel;
use app\admin\model\User;

/**
 * 社区管理
 *
 * @icon   fa fa-list
 */
class Community extends Backend
{

    protected $model = null;
    protected $categorylist = [];
    protected $noNeedRight = ['selectpage'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new CommunityModel();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams('title');
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $user =  User::where(['id'=>$v['user_id']])->find();
                $list[$k]['nickname'] = $user['nickname'];
                $list[$k]['imgs'] = explode(',',$v['imgs']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    //精选
    public function setRecommendTime()
    {
        if ($this->request->isPost()) {
            $data = $this->request->request();
            if(!$data['type']){
                $this->error('参数错误');
            }
            if($data['type'] == 1){
                $recommend_time = time();
            }else{
                $recommend_time = 0;
            }
            $result = CommunityModel::where(['id'=>$data['id']])->update(['recommend_time'=>$recommend_time]);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('操作失败');
            }
        }
    }
    
    //上/下架
    public function setStatus()
    {
        if ($this->request->isPost()) {
            $data = $this->request->request();
            if(!$data['id']){
                $this->error('参数错误');
            }
            $result = CommunityModel::where(['id'=>$data['id']])->update(['status'=>$data['status']]);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('操作失败');
            }
        }
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->request();
            if(!$data['id']){
                $this->error('参数错误');
            }
            $result = CommunityModel::where(['id'=>$data['id']])->update(['status'=>$data['status']]);
            if ($result !== false) {
                $this->success();
            } else {
                $this->error('操作失败');
            }
        }
        return parent::add();
    }

    /**
     * Selectpage搜索
     *
     * @internal
     */
    public function selectpage()
    {
        return parent::selectpage();
    }



}
